{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 高阶函数\n",
    "把函数作为参数传入，这样的函数称为高阶函数"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### prc1.1 求取列表中每个元素的平方值（普通求法）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
     ]
    }
   ],
   "source": [
    "def func01(num):\n",
    "    double = num*num\n",
    "    return double\n",
    "\n",
    "def func02(func,lst):\n",
    "    for i in range(len(lst)):\n",
    "        lst[i] = func(lst[i])\n",
    "    return lst\n",
    "\n",
    "lst = [i for i in range(10)]\n",
    "print(func02(func01,lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 一、map()\n",
    "#### prc1.2 求取列表中每个元素的平方值（map求法）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8]\n",
      "[10, 12, 14, 16, 18]\n",
      "[0, 4, 16, 36, 64]\n",
      "[0, 24, 56, 96, 144]\n"
     ]
    }
   ],
   "source": [
    "def func1(item):\n",
    "    return item*item\n",
    "\n",
    "def func2(item1,item2):\n",
    "    return item1*item2\n",
    "\n",
    "lst = [i for i in range(0,10,2)]\n",
    "print(lst)\n",
    "lst1 = [i for i in range(10,20,2)]\n",
    "print(lst1)\n",
    "\n",
    "print(list(map(func1,lst)))\n",
    "#当map有两个可迭代对象或者说当输入函数有两个参数时：\n",
    "print(list(map(func2,lst,lst1)))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Prc2:lambda练习"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1.34', '1.23', '3.57']\n",
      "['3.46%', '0.77%', '88.88%', '30.00%']\n",
      "['1', '2', '3', '4']\n",
      "[2, 3, 0, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "#2.1 保留两位小数\n",
    "lst1 = [1.3423434, 1.234234656,3.567456]\n",
    "\n",
    "#2.2 准化为百分比输出，并且保留两位小数\n",
    "#0.034556 -- 》3.46%\n",
    "lst2 = [0.034556, 0.007657, 0.8888, 0.3]\n",
    "\n",
    "#2.3 将a里的每个数字转化为字符串形式\n",
    "lst3 = [1,2,3,4]\n",
    "\n",
    "#2.4 输出d字符串中每个对应的ascii码值\n",
    "d = \"span\"\n",
    "#对列表中的每个元素取绝对值\n",
    "lst4 = [-2,-3,0,5,-6]\n",
    "\n",
    "print(list(map(lambda item:\"%.2f\" % item,lst1)))\n",
    "print(list(map(lambda item:\"%.2f%%\" % (item * 100),lst2)))\n",
    "print(list(map(lambda item:str(item),lst3)))\n",
    "print(list(map(lambda item:abs(item),lst4)))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 二、过滤（filter）\n",
    "取出10以内的奇数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "print(list(filter(lambda x:x%2 == 1,range(10))))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1234567\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "[153, 370, 371, 407]\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "print(list(filter(lambda x:math.sqrt(x)%1 == 0,range(1,101))))\n",
    "print(list(filter(lambda x:(x//100)**3+(x//10%10)**3+(x%10)**3 == x,range(100,999))))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "lst = 'bobabout'\n",
    "print(list(map(lambda x:math.sqrt(x)%1 == 0,range(1,101))))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "execution_count": 90,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[True, False, False, True, False, False, False, False, True, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True]\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 三、sorted()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "lst=[(1,2),(2,2),(1,0),(2,4),(True,False)]\n",
    "print(sorted(lst))\n",
    "#字典按值排序\n",
    "d1 = {\"a\":2,\"d\":1,\"b\":3,\"c\":5}\n",
    "print(sorted(d1.items())) #不行，按键排序了\n",
    "print(dict(sorted(d1.items(),key=lambda x:x[1]))) #搞定"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 四、reduce()\n",
    "Prc：实现python的int功能\n",
    "\n",
    "如'123' --> 123"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> 1234567\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "#字符串'123'转换成一个整数123\n",
    "#两个步骤：\n",
    "#第一步:先将字符串'123'中的每一个字符'1','2','3'都转换成整数1,2,3。适用于map函数。\n",
    "#第二步：再将每一个数字进行相应的处理，使其成为整数123,适用于reduce函数\n",
    "\n",
    "#整体进行封装一下，使其使用起来和int函数一样方便\n",
    "def custom_int(string):\n",
    "    #单字符转单整数\n",
    "    def char_to_num(string):\n",
    "        all_number_dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}\n",
    "        # 以参数string为键，取出all_number_dict里面的值\n",
    "        return all_number_dict[string]\n",
    "    #单整数计算成整数\n",
    "    def one_to_all(x, y):\n",
    "        res = x * 10 + y\n",
    "        return res\n",
    "    #判断是否为数字\n",
    "    return reduce(one_to_all,map(char_to_num,string))\n",
    "\n",
    "a = '1234567'\n",
    "print(type(custom_int(a)),custom_int(a))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 改进点:\n",
    "1.能否判断是否是字母，若包含字母则报错"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}